package com.sheyu.mall.product.service.category.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sheyu.common.exception.RRException;
import com.sheyu.mall.product.dao.brand.BrandDao;
import com.sheyu.mall.product.dao.category.CategoryDao;
import com.sheyu.mall.product.entity.brand.BrandEntity;
import com.sheyu.mall.product.entity.category.CategoryEntity;
import com.sheyu.mall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.Query;

import com.sheyu.mall.product.dao.category.CategoryBrandRelationDao;
import com.sheyu.mall.product.entity.category.CategoryBrandRelationEntity;
import com.sheyu.mall.product.service.category.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存分类与品牌的数据
     * @param categoryBrandRelation
     */
    @Transactional
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        BrandEntity brandEntity = brandDao.selectById(brandId);
        if (brandEntity!=null) {
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        if (categoryEntity!=null) {
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        boolean saveResult = this.save(categoryBrandRelation);
        if (saveResult==false){
            throw new RRException("更新分类与品牌的数据失败");
        }
    }

    @Transactional
    @Override
    public void updateBrand(Long brandId, String name) {
        //对数据库中存在的brandId进行判断是否存在
        long primaryId = categoryBrandRelationDao.selectByBrandId(brandId);
        if (primaryId==1){
            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setBrandName(name);
            categoryBrandRelationEntity.setBrandId(brandId);
            //this.updateById(categoryBrandRelationEntity);

            boolean updateResult = this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>()
                    .eq("brand_id", brandId));
            if(updateResult==false){
                throw new RRException("categoryBrandRelationEntity通过brandId更新失败");
            }
        }else{
            throw new RRException("更新时，对应的品牌id不存在"+brandId);
        }

    }

    @Transactional
    @Override
    public void updateCategory(Long catId, String name) {
        long primaryId = categoryBrandRelationDao.selectByCatIdBySQL(catId);
        if (primaryId==1){
            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setCatelogName(name);
            categoryBrandRelationEntity.setCatelogId(catId);
            //this.updateById(categoryBrandRelationEntity);
            boolean updateResult = this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>()
                    .eq("catelog_id", catId));
            if (updateResult){
                throw new RRException("categoryBrandRelationEntity通过catId更新失败");
            }
        }

    }

    @Override
    public List<BrandVo> getBrandsList(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.list(wrapper);

        List<Long>  brandIds = categoryBrandRelationEntities.stream().map((item)->{
            return item.getBrandId();
        }).collect(Collectors.toList());
        if (brandIds != null && brandIds.size()>0) {
            List<BrandEntity> brandEntities = brandDao.selectBatchIds(brandIds);
//            List<BrandEntity> brandEntities1 = brandDao.selectBatchIdsBySQL(brandIds);
            List<BrandVo> brandVos = brandEntities.stream().map((obj) -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(obj.getBrandId());
                brandVo.setBrandName(obj.getName());
                return brandVo;
            }).collect(Collectors.toList());
            return brandVos;
        }else{
            throw new RRException("对应的品牌不存在");
        }
    }

    @Override
    public List<CategoryBrandRelationEntity> getResultByBradId(Long brandId) {

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList
                (new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
//        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = baseMapper.selectListBySQL(brandId);
        return categoryBrandRelationEntities;
    }
}
